package com.talent.jiwen;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.SmsResult;
import com.talent.jiwen.util.AMUtils;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;
import com.talent.jiwen.util.downtime.DownTimer;
import com.talent.jiwen.util.downtime.DownTimerListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;

import static com.talent.jiwen.util.Constant.FORGET_PWD_CODE_METHOD;


/**
 * Created by qyqx on 16/2/2.
 * 更改密码
 */
@SuppressWarnings("deprecation")
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener, DownTimerListener {


    private EditText mCode, mPassword1, mPassword2;
    private TextView mPhone;
    private Button mGetCode, mOK;
    private String phone;
    private static final int CHANGE_PASSWORD_BACK = 1002;

    @BindView(R.id.iv_new_hide)
    ImageView mIvNew;
    @BindView(R.id.iv_confirm_hide)
    ImageView mIvConfirm;

    boolean isHide=true;
    boolean isConfirmHide=true;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    public String titleName() {
        return "更改密码";
    }

    @Override
    protected void initData() {
//        StatusBarUtil.setTranslucent(this);
//        setHeadVisibility(View.GONE);
        initView();
    }


    private void initView() {
        mPhone = (TextView) findViewById(R.id.forget_phone);
        mCode = (EditText) findViewById(R.id.forget_code);
        mPassword1 = (EditText) findViewById(R.id.forget_password);
        mPassword2 = (EditText) findViewById(R.id.forget_password1);
        mGetCode = (Button) findViewById(R.id.forget_getcode);
        mOK = (Button) findViewById(R.id.forget_button);
        mGetCode.setOnClickListener(this);
        mOK.setOnClickListener(this);
        mIvNew.setOnClickListener(this);
        mIvConfirm.setOnClickListener(this);

        phone = SPUtil.getUserPhone();
        mPhone.setText(phone);
        mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
//        mPhone.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if (s.length() == 11) {
//                    if (AMUtils.isMobile(s.toString().trim())) {
//                        phone = mPhone.getText().toString().trim();
//                        mGetCode.setClickable(true);
//                        mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
//                        AMUtils.onInactive(mContext, mPhone);
//                    } else {
//                        Toast.makeText(mContext, R.string.Illegal_phone_number, Toast.LENGTH_SHORT).show();
//                    }
//                } else {
//                    mGetCode.setClickable(false);
//                    mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_gray));
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });

        mCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 4) {
                    AMUtils.onInactive(mContext, mCode);
                        mOK.setClickable(true);
                        mOK.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_getcode:
                if (TextUtils.isEmpty(mPhone.getText().toString().trim())) {
                    showToast(getString(R.string.phone_number_is_null));
                } else {
                    DownTimer downTimer = new DownTimer();
                    downTimer.setListener(this);
                    downTimer.startDown(60 * 1000);
                    sendCode();
                }
                break;
            case R.id.forget_button:
                if (TextUtils.isEmpty(mPhone.getText().toString())) {
                    showToast(getString(R.string.phone_number_is_null));
//                    mPhone.setShakeAnimation();
                    return;
                }

                if (TextUtils.isEmpty(mCode.getText().toString())) {
                    showToast(getString(R.string.code_is_null));
//                    mCode.setShakeAnimation();
                    return;
                }

                if (TextUtils.isEmpty(mPassword1.getText().toString())) {
                    showToast(getString(R.string.password_is_null));
//                    mPassword1.setShakeAnimation();
                    return;
                }

                if (mPassword1.length() < 6 || mPassword1.length() > 16) {
                    showToast(getString( R.string.passwords_invalid));
                    return;
                }

                if (TextUtils.isEmpty(mPassword2.getText().toString())) {
                    showToast(getString( R.string.confirm_password));
//                    mPassword2.setShakeAnimation();
                    return;
                }

                if (!mPassword2.getText().toString().equals(mPassword1.getText().toString())) {
                    showToast( getString(R.string.passwords_do_not_match));
                    return;
                }

//                LoadDialog.show(mContext);
                forgrtPwd();

                break;

            case R.id.iv_new_hide:
                String pwd1=mPassword1.getText().toString().trim();
                isHide = !isHide;
                if (isHide) {
                    mIvNew.setImageResource(R.mipmap.forgetpwd_hide);
                    mPassword1.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }else {
                    mIvNew.setImageResource(R.mipmap.forgetpwd_show);
                    mPassword1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }

                if(!Validators.isEmpty(pwd1)){
                    mPassword1.setSelection(pwd1.length());
                }

                break;
            case R.id.iv_confirm_hide:
                String pwd2=mPassword2.getText().toString().trim();
                isConfirmHide = !isConfirmHide;
                if (isConfirmHide) {
                    mIvConfirm.setImageResource(R.mipmap.forgetpwd_hide);
                    mPassword2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }else {
                    mIvConfirm.setImageResource(R.mipmap.forgetpwd_show);
                    mPassword2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

                }
                if(!Validators.isEmpty(pwd2)){
                    mPassword2.setSelection(pwd2.length());
                }

                break;
        }
    }

    private void sendCode(){
        Map<String, String> map = new HashMap<>();
        map.put("method", FORGET_PWD_CODE_METHOD);
        map.put("phone", phone);
        Observable observable = ApiFactory.getEducationApiSingleton().sendMsg(map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<SmsResult>(this) {
            @Override
            protected void _onNext(SmsResult smsResult) {
//                if(smsResult!=null){
//                    String code=smsResult.getVcode();
//                    if(Validators.isEmpty(code)){
//                        showToast(getString(R.string.code_get_fill));
//                    }
//                }else{
//                    showToast(getString(R.string.data_is_wrong));
//                }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);

    }


    private void forgrtPwd(){

        Map<String, String> map = new HashMap<>();
        map.put("phone", mPhone.getText().toString());
        map.put("password", mPassword1.getText().toString());
        map.put("vCode", mCode.getText().toString());

        Observable observable = ApiFactory.getEducationApiSingleton().resetPassword(map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(this) {
            @Override
            protected void _onNext(Void v) {
//                Intent data = new Intent();
//                data.putExtra("phone", phone);
//                data.putExtra("password", mPassword1.getText().toString());
//                setResult(CHANGE_PASSWORD_BACK, data);
                showToast("更改成功");
                finish();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,true);


    }


    @Override
    public void onTick(long millisUntilFinished) {
        mGetCode.setText(String.valueOf(millisUntilFinished / 1000) + "s");
        mGetCode.setClickable(false);
        mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_gray));
    }

    @Override
    public void onFinish() {
        mGetCode.setText(R.string.get_code);
        mGetCode.setClickable(true);
        mGetCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.rs_select_btn_blue));
    }

    @Override
    public void onMinTick(String str) {

    }
}
